package lib

import (
	"encoding/json"

	"git.oschina.net/Ljohn/softCup/models"
	"git.oschina.net/Ljohn/softCup/conf"
)

type JSONObj struct {
	data interface{}
}

//Json Initialize the json configruation
func Json(data string) *JSONObj {
	j := new(JSONObj)
	var f interface{}
	err := json.Unmarshal([]byte(data), &f)
	if err != nil {
		return j
	}
	j.data = f
	return j
}

//GetGitLabModel ...
func (j *JSONObj) GetGitlabProjectModel() []*models.GitlabProject {
	modelMap := make([]*models.GitlabProject, 0)
	for k, _ := range (j.data).([]interface{}) {
		model := &models.GitlabProject{}
		if m, ok := (j.data).([]interface{}); ok {
			v := m[k].(map[string]interface{})
			if h, ok := v["name_with_namespace"]; ok {
				model.Name_with_namespace = h.(string)
			}
			if h, ok := v["http_url_to_repo"]; ok {
				model.Http_url_to_repo = h.(string)
			}
			if h, ok := v["public"]; ok {
				model.Public = h.(bool)
			}
			if h, ok := v["id"]; ok {
				model.Id = h.(float64)
			}
		}
		model.Addr= conf.GitlabURL()
		modelMap = append(modelMap, model)
	}
	return modelMap
}
